package com.imarkket.Library;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.imarkket.R;
import com.imarkket.sqlite.model.t_lista;

/**
 * Created by Sistemas on 23/05/2014.
 */

public class c_shopping_list_adapter extends BaseAdapter {
    private final Activity actividad;
    private final String[][] MatrizBidimensional;
    private final int CountTotal;

    public c_shopping_list_adapter(Activity actividad, int Total, String[][] pMatrizBidimensional) {
        super();
        c_logging.getInstance().log(c_logging.LOG_INFO, "CONSTRUCTOR: Total: " + Total );
        this.CountTotal = Total;
        this.actividad = actividad;
        this.MatrizBidimensional = pMatrizBidimensional;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        //c_logging.getInstance().log(c_logging.LOG_INFO, "Position: " + position + "; convertView = " + convertView + "; parent=" + parent);

        LayoutInflater inflater = actividad.getLayoutInflater();

        // Hace referencia al fichero xml shopping_list_item.xml
        View view = inflater.inflate(R.layout.l_shopping_list_item, null, true);

        String KEY_NCOD_INTERNO = this.MatrizBidimensional[position][0];

        String KEY_ADES_HASH = this.MatrizBidimensional[position][1];
        String KEY_ADES_LARGA = this.MatrizBidimensional[position][2];

        String KEY_NCOD_SHARED = this.MatrizBidimensional[position][3];
        String KEY_NCOD_COUNT = this.MatrizBidimensional[position][4];
        String KEY_NCOD_RATING = this.MatrizBidimensional[position][5];

        String KEY_DFEC_CREACION = this.MatrizBidimensional[position][6];
        String KEY_DFEC_UPDATED = this.MatrizBidimensional[position][7];

        TextView description = (TextView) view.findViewById(R.id.titulo);
        description.setText(KEY_ADES_LARGA);

        TextView created = (TextView) view.findViewById(R.id.created);
        created.setText(KEY_DFEC_CREACION);

        TextView updated = (TextView) view.findViewById(R.id.updated);
        updated.setText(KEY_DFEC_UPDATED);

        TextView elements = (TextView) view.findViewById(R.id.elements);
        elements.setText(KEY_NCOD_COUNT);

        ImageView shared = (ImageView) view.findViewById(R.id.shared);
        if (KEY_NCOD_SHARED.equals("1"))
            shared.setImageResource(R.drawable.multiusers);
        else
            shared.setImageResource(R.drawable.user);

        // Imágenes según la cantidad de elementos
        ImageView imageicon = (ImageView) view.findViewById(R.id.icono);
        if (Integer.parseInt(KEY_NCOD_COUNT) > 0)
            imageicon.setImageResource(R.drawable.basketfull);
        else
            imageicon.setImageResource(R.drawable.basketempty);

        return view;
    }

    public int getCount() {
        return this.CountTotal;
    }

    @Override
    public Object getItem(int position) {
        // Obtenemos el objeto KEY_ADES_HASH de la posición "position"
        return this.MatrizBidimensional[position][1];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}